import apis from "./moniPushApi";

export default {
  namespace: "moniPush",
  state: {
    pushList: [],
    errCode: "-1",
    errMsg: "",
    loading: false,
    totalCount: 0
  },
  // subscriptions: {
  //   setup({ dispatch, history }) {
  //     // ...
  //   }
  // },
  effects: {
    // 推送数量趋势图
    *getPushData({ payload }, { call, put }) {
      yield put({
        type: "put/putloading",
        payload: true
      });
      const res = yield call(apis.pushList, payload);
      const { data, totalCount } = res;
      yield put({
        type: "get/getPushData",
        payload: data
      });
      yield put({
        type: "put/putloading",
        payload: false
      });
      yield put({
        type: "put/puttotal",
        payload: totalCount
      });
    },
    *pushMsg({ payload }, { call, put }) {
      const res = yield call(apis.pushMsg, payload);
      const { errCode, errMsg } = res;
      yield put({
        type: "get/geterrCode",
        payload: errCode
      });
      yield put({
        type: "get/geterrMsg",
        payload: errMsg
      });
    }
  },
  reducers: {
    "get/getPushData": (state, { payload }) => ({
      ...state,
      pushList: payload
    }),
    "get/geterrCode": (state, { payload }) => ({
      ...state,
      errCode: payload
    }),
    "put/putloading": (state, { payload }) => ({
      ...state,
      loading: payload
    }),
    "put/puttotal": (state, { payload }) => ({
      ...state,
      totalCount: payload
    }),
    "get/geterrMsg": (state, { payload }) => ({
      ...state,
      errMsg: payload
    })
  }
};
